import Vue from "vue";
import VueRouter from "vue-router";
import index from "@/views/index/index.vue";
import login from "@/views/login/login.vue";
import search from "@/views/search/search.vue";
import searchList from "@/views/searchList/searchList.vue";
import detail from "@/views/detail/detail.vue";
import pay from "@/views/pay/pay.vue";
import order from "@/views/order/order.vue";
import IndexIndex from "@/views/index/index-index.vue";
import indexClass from "@/views/index/index-class.vue";
import indexCart from "@/views/index/index-cart.vue";
import indexMy from "@/views/index/index-my.vue";
import store from "@/store";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    component: index,
    redirect: "/index",
    children: [
      {
        path: "/index",
        component: IndexIndex,
      },
      {
        path: "/class",
        component: indexClass,
      },
      {
        path: "/cart",
        component: indexCart,
      },
      {
        path: "/my",
        component: indexMy,
      },
    ],
  },
  {
    path: "/login",
    component: login,
  },
  {
    path: "/search",
    component: search,
  },
  {
    path: "/searchList",
    component: searchList,
  },
  {
    path: "/detail/:goodsId",
    component: detail,
  },
  {
    path: "/pay",
    component: pay,
  },
  {
    path: "/order",
    component: order,
  },
];

const router = new VueRouter({
  routes,
});

const Mypath = ["/pay", "/order", "/cart"];

router.beforeEach((to, from, next) => {
  if (!Mypath.includes(to.path)) {
    next();
    return;
  }
  const token = store.getters.token;
  if (token) {
    next();
  } else {
    next("/login");
  }
});

export default router;
